package com.oa.srms.apps.actions.staff;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.oa.srms.apps.actions.BasicAction;
import com.oa.srms.apps.business.limit.ILimitInfoService;
import com.oa.srms.apps.business.limit.IPostLimitService;
import com.oa.srms.apps.business.organization.IPostInfoService;
import com.oa.srms.apps.business.organization.ISectionInfoService;
import com.oa.srms.apps.business.staff.IStaffInfoService;
import com.oa.srms.apps.constants.IDEDStates;
import com.oa.srms.apps.entity.organization.PostInfoValue;
import com.oa.srms.apps.entity.organization.SectionInfoValue;
import com.oa.srms.apps.entity.staff.StaffInfoValue;

@SuppressWarnings("serial")
public class StaffInfoAction extends BasicAction {
	
	@Autowired
	private IStaffInfoService staffInfoService;
	@Autowired
	private IPostLimitService postLimitService;
	@Autowired
	private ILimitInfoService limitInfoService;
	@Autowired
	private ISectionInfoService sectionInfoService;
	@Autowired
	private IPostInfoService postService; 
	
	private String staffId;
	private String staffIds;
	private String staffNo;
	private String staffPwd;
	private String newStaffPwd;
	private String sectionId;
	private String staffName;
	private String postId;
	private String states;
	private String str;
	private StaffInfoValue staffInfo;
	private List<SectionInfoValue> sectionList;
	private List<PostInfoValue> postList;
	private List<StaffInfoValue> staffList;
	
	
	public String toEditPwd() throws Exception {
		staffInfo = (StaffInfoValue)session.get("login_staffInfo");
		return "toEditPwdPage";
	}
	
	public String toAdd() throws Exception {
		sectionList = sectionInfoService.querySectionInfo(null, IDEDStates.ENABLE);
		return "toAdd";
	}
	
	public String toQuery() throws Exception {
		sectionList = sectionInfoService.querySectionInfo(null, IDEDStates.ENABLE);
		
		Integer postId_INT = null;
		Integer states_INT = null;
		if("".equals(staffName)){
			staffName = null;
		}
		if(!"-1".equals(postId) && postId != null && !"".equals(postId)){
			postId_INT = Integer.parseInt(postId);
			postList = postService.queryPostInfoBySectionId(Integer.parseInt(sectionId));
		}else{
			postId_INT = null;
			sectionId = null;
		}
		if(states != null && !"".equals(states)){
			states_INT = Integer.parseInt(states);
		}else{
			states_INT = null;
		}
		
		totalpage = staffInfoService.getTotalPageNumber(staffName, postId_INT, states_INT, rows);
		count = staffInfoService.getTotatNumber();
		staffList = staffInfoService.queryStaffInfo(staffName, postId_INT, states_INT, rows, currpage);
		return "toQueryPage";
	}
	
	public String toModify() throws Exception {
		staffInfo = staffInfoService.queryByStaffId(Integer.parseInt(staffId));
		
		sectionList = sectionInfoService.querySectionInfo(null, IDEDStates.ENABLE);
		
		sectionId = staffInfo.getPostInfo().getSectionInfo().getSectionId().toString();
		postId = staffInfo.getPostInfo().getPostId().toString();
		postList = postService.queryPostInfoBySectionId(Integer.parseInt(sectionId));
		
		return "toModify";
	}
	
	public String toQueryStaff() throws Exception {
		str ="";
		staffInfo = (StaffInfoValue)session.get("login_staffInfo");
		String limitIds = postLimitService.queryByPostId(staffInfo.getPostInfo().getPostId()).getLimitIds();
		for(String limitId : limitIds.split(",")){
			str += limitInfoService.queryByLimitId(Integer.parseInt(limitId)).getLimitName()+", ";
		}
		str = str.substring(0, str.length()-2);
		return "toQueryStaff";
	}
	
	public String modifyPwd() throws Exception{
		staffInfo = staffInfoService.verifyStaffInfo(staffNo, staffPwd);
		if (staffInfo != null) {
			staffInfoService.modify(staffInfo.getStaffId(), null, newStaffPwd, null);
			return "toLogout";
		}
		str="修改失败";
		return "toEditPwd";
	}
	
	public String delete() throws Exception{
		if(staffId != null)
		staffInfoService.delete(Integer.parseInt(staffId));
		if(staffIds != null){
			String[] staffArr = staffIds.split(",");
			for(String staffItem : staffArr){
				staffInfoService.delete(Integer.parseInt(staffItem));
			}
		}
		return "toQuery";
	}
	
	public String addStaff() throws Exception {
		staffInfoService.add(staffNo, staffName, "123456", Integer.parseInt(postId));
		clear();
		return "toAddPage";
	}
	
	public String modifyStaff() throws Exception{
		staffPwd = null;
		staffInfoService.modify(Integer.parseInt(staffId), staffName, staffPwd, Integer.parseInt(postId));
		clear();
		return "toQuery";
	}
	
	public String toEnable() throws Exception {
		staffInfoService.toEnable(Integer.parseInt(staffId));
		return "toQuery";
	}
	
	public String toDisable() throws Exception {
		staffInfoService.toDisable(Integer.parseInt(staffId));
		return "toQuery";
	}
	
	private void clear(){
		staffNo = null;
		staffPwd = null;
		staffName = null;
		sectionId = null;
		postId = null;
	}

	public String checkStaff() throws Exception {
		staffInfo = staffInfoService.verifyStaffInfo(staffNo, staffPwd);
		String result = "";
		if (staffInfo != null) {
			session.put("login_staffInfo", staffInfo);
			result = "ok";
		} else{
			result = "no";
		}
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		response.getWriter().write(result);
		response.flushBuffer();
		return null;
	}
	
	public String queryPostBySection() throws Exception {
		if (sectionId != null) {
			postList = postService.queryPostInfoBySectionId(Integer.parseInt(sectionId));
			response.setContentType("text/html; charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			this.str = getPostOptionsFrom(postList);
			response.getWriter().print(str);
		}
		return null;
	}
	
	private String getPostOptionsFrom(List<PostInfoValue> list) {
		StringBuilder sb = new StringBuilder();
		sb.append("<option value=\"-1\">-请选择-</option>");
		for (PostInfoValue piv : list) {
			String temp = "<option value=\"" + piv.getPostId().toString()
					+ "\">" + piv.getPostName() + "</option>";
			sb.append(temp);
		}
		return sb.toString();
	}
	
	public String verifyStaffNo() throws Exception{
		if(staffInfoService.verifyStaffNo(staffNo) == true)
			str = "1";
		else 
			str = "0";
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		response.getWriter().print(str);
		response.getWriter().close();
		return null;
	}

	public String getStaffNo() {
		return staffNo;
	}

	public void setStaffNo(String staffNo) {
		this.staffNo = staffNo;
	}

	public String getStaffPwd() {
		return staffPwd;
	}

	public void setStaffPwd(String staffPwd) {
		this.staffPwd = staffPwd;
	}

	public List<SectionInfoValue> getSectionList() {
		return sectionList;
	}

	public void setSectionList(List<SectionInfoValue> sectionList) {
		this.sectionList = sectionList;
	}

	public String getSectionId() {
		return sectionId;
	}

	public void setSectionId(String sectionId) {
		this.sectionId = sectionId;
	}

	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}

	public List<PostInfoValue> getPostList() {
		return postList;
	}

	public void setPostList(List<PostInfoValue> postList) {
		this.postList = postList;
	}

	public String getPostId() {
		return postId;
	}

	public void setPostId(String postId) {
		this.postId = postId;
	}

	public String getStaffName() {
		return staffName;
	}

	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}

	public StaffInfoValue getStaffInfo() {
		return staffInfo;
	}

	public void setStaffInfo(StaffInfoValue staffInfo) {
		this.staffInfo = staffInfo;
	}

	public String getStaffId() {
		return staffId;
	}

	public void setStaffId(String staffId) {
		this.staffId = staffId;
	}

	public List<StaffInfoValue> getStaffList() {
		return staffList;
	}

	public void setStaffList(List<StaffInfoValue> staffList) {
		this.staffList = staffList;
	}

	public String getStates() {
		return states;
	}

	public void setStates(String states) {
		this.states = states;
	}

	public String getStaffIds() {
		return staffIds;
	}

	public void setStaffIds(String staffIds) {
		this.staffIds = staffIds;
	}

	public String getNewStaffPwd() {
		return newStaffPwd;
	}

	public void setNewStaffPwd(String newStaffPwd) {
		this.newStaffPwd = newStaffPwd;
	}
	
	
}
